<?php
use Phalcon\Tag as Tag;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;


class BusinessUsersController extends ControllerBase
{
    public function initialize()
    {
        Tag::setTitle(' 业务系统用户');
        parent::initialize();
    }

    public function indexAction(){
        $this->session->conditions = null;
        $this->view->form = new BusinessUsersForm();
    }

    /**
     * 创建业务用户表单
     *
     * @param string $id
     */
    public function newAction($id)
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        $businessUsers = new BusinessUsers();
        $serverUserName = $businessUsers->getUserNameById($id);
        $this->view->form = new BusinessUsersForm(null, array('edit' => true,'serverUserName' => $serverUserName,'serverUserId'=>$id));
    }

    /**
     * new之后保存业务用户表单数据
     *
     * @return mixed
     */
    public function createAction()
    {
        if (!$this->request->isPost())
        {
            return $this->forward('businessusers/index');
        }

        $form = new BusinessUsersForm();
        $BusinessSystem = new BusinessUsers();
        $data = $this->request->getPost();
        if (!$form->isValid($data, $BusinessSystem))
        {
            foreach ($form->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('businessusers/new/'.$data['serverUserId']);
        }

        $data['addTime'] = $data['updateTime'] = date("Y-m-d H:i:s");
        if ($BusinessSystem->save($data, array('user', 'serverUserId', 'businessSystemId', 'addTime', 'updateTime')) == false)
        {
            foreach ($BusinessSystem->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('businessusers/new/'.$data['serverUserId']);
        }

        $form->clear();
        $this->flash->success("业务用户保存成功！");
        return $this->forward("businessusers/search");
    }

    /**
     * 查询业务用户
     *
     * @return mixed
     */
    public function searchAction()
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        $numberPage = 1;
        $users = BusinessUsers::find();
        if (count($users) == 0)
        {
            $this->flash->notice("业务用户没有数据！");
            return $this->forward("index/index");
        }

        $system = new Paginator(array(
            "data"  => $users,
            "limit" => 10,
            "page"  => $numberPage
        ));

        $this->view->page = $users;
        //$this->view->page = $paginator->getPaginate();

    }

    /**
     * 修改业务用户
     *
     * @param $id
     * @param $uid
     * @return mixed
     */
    public function editAction($id, $uid)
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        if (!$this->request->isPost())
        {
            $businessUserId = BusinessUsers::findFirstById($id);
            if (!$businessUserId)
            {
                $this->flash->error("业务用户修改失败！");
                return $this->forward("businessusers/index");
            }
            $this->view->form = new BusinessUsersForm($businessUserId, array('edit' => true));
            $this->view->setVars(array("uid" => $uid));
        }

    }

    /**
     * editAction之后保存
     *
     * @return mixed
     */
    public function saveAction()
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        if(!$this->request->isPost())
        {
            $this->flash->error("提交方式不是POST");
            return $this->forward('businessusers/index');
        }

        $id = $this->request->getPost("id", "int");
        $serverUserId = $this->request->getPost("serverUserId", "int");
        $businessUserId = BusinessUsers::findFirstById($id);
        if(!$businessUserId)
        {
            $this->flash->error("业务用户没有信息数据！");
            return $this->forward("businesssystems/index");
        }

        $form = new BusinessUsersForm($businessUserId, array('edit' => true));
        $this->view->form = $form;
        $data = $this->request->getPost();

        if (!$form->isValid($data, $businessUserId))
        {
            foreach ($form->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            echo "businesssystems/edit/' . $id";
            return $this->forward('businesssystems/edit/' . $id);
        }

        if ($businessUserId->save() == false)
        {
            foreach ($businessUserId->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('businesssystems/edit/' . $id);
        }
        $form->clear();
        $this->flash->success("业务用户保存成功！");
        return $this->forward("businessusers/userSearch/". $serverUserId);
    }

    /**
     * 删除业务用户
     *
     * @param $id
     * @return mixed
     */
    public function deleteAction($id)
    {
        $userId = BusinessUsers::findFirstById($id);
        if (!$userId)
        {
            $this->flash->error("业务用户未找到！");
            return $this->forward("businessusers/index");
        }

        if (!$userId->delete())
        {
            foreach ($userId->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward("businessusers/search");
        }
        $this->flash->success("业务用户删除成功！");
        return parent::forward("businessusers/search");
    }

    public function userNewAction()
    {

    }

    /**
     * 查询业务用户信息
     */
    public function userNewListAction()
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        $businessUsers = new BusinessUsers();
        $businessUsers->searchServerUsers();
    }

    public function userEditAction()
    {

    }

    public function userEditListAction()
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        $businessUsers = new BusinessUsers();
        $businessUsers->searchServerUsers();
    }

    public function userSearchAction($id)
    {
        $this->view->setTemplateAfter('businessUsersIframe');  //重新设置视图模版
        $numberPage = 1;
        $conditions = "serverUserId = :serverUserId: "; //模型查询绑定数据
        $parameters = array('serverUserId' => "$id");   //参数
        $users = BusinessUsers::find(array($conditions, "bind"=>$parameters));

        if (count($users) == 0)
        {
            $this->flash->notice("业务用户没有数据!!!!！");
        }

        $system = new Paginator(array(
            "data"  => $users,
            "limit" => 10,
            "page"  => $numberPage
        ));

        $this->view->page = $users;
        //$this->view->page = $paginator->getPaginate();
    }

}